//
//  JYWelcomViewController.swift
//  01.weibo
//
//  Created by 谢玉锋 on 16/7/13.
//  Copyright © 2016年 itheima.cn. All rights reserved.
//

import UIKit
import SnapKit//用来布局控件
import SDWebImage

class JYWelcomViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        //准备UI
        prepareUI()
        
        self.setIcon()
        //获取用户信息的网络请求
        JYUserAccountViewModel.sharaInstance.loadUserInfo { (error) -> () in
            if error != nil {
                JYPrint("获取用户信息出错了: \(error)")
                return
            }
            
            // 获取用户信息成功了
            self.setIcon()
        }
        
    }
    //设置头像
    private func setIcon () {
        if let avatar_large = JYUserAccountViewModel.sharaInstance.userAccount?.avatar_large {
        let url = NSURL(string: avatar_large)
        self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
    }
    
    
    private func prepareUI () {
    
        //添加到父控件(注意这里是在控制器中,是控制器的view添加子控件)
        view.addSubview(background)
        view.addSubview(iconView)
        view.addSubview(label)
        //添加约束
        //1.首先把自适应去掉,代码约束时,系统会自动帮我们添加约束,所以,需要禁用系统的,才不会对我们的造成影响
        background.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        label.translatesAutoresizingMaskIntoConstraints = false
        
        background.snp_makeConstraints { (make) -> Void in
//            make.leading.equalTo(view)
//            make.top.equalTo(view)
//            make.trailing.equalTo(view)
//            make.bottom.equalTo(view)
            //缩写
            make.leading.top.trailing.bottom.equalTo(view)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.width.height.equalTo(85)
            make.bottom.equalTo(view).offset(-160)//注意offset是相对view的底部偏移,所以是-160
        }
        
        label.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    //当view显示的时候才开始动漫,否者看不到
    override func viewDidAppear(animated: Bool) {
        iconViewAnimation()
    }
    
    //动画效果
    private func iconViewAnimation() {
    //1.修改约束
        iconView.snp_updateConstraints { (make) -> Void in
            //从下面偏移
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
    //2.设置动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //让需要重新布局的实现动画
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                //动画完成处理的事情 (让label显示)
                UIView.animateWithDuration(DefaultAnimationTime, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
//                  //切换控制器
                    let controller = JYMainTabBarController()
                    
                    (UIApplication.sharedApplication().delegate as! AppDelegate).switchController(controller)
                })
        } 
    }
    
    
    
    
    //懒加载子控件
    //背景
    private lazy var background: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    
    private lazy var iconView: UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        //裁剪
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        icon.sizeToFit()
        return icon
    }()
    
    //label
    private lazy var label: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(labelFont)
        //注意: 根据字体大小自适应,label在没有约束情况下不会显示
        label.sizeToFit()
        label.alpha = 0
        return label
    }()

}
